package reflect;

import java.lang.reflect.Field;
import java.util.Arrays;

public class ReflectDemo9 {
    public static void main(String[] args) throws Exception {
        String s1 = "hello";//创建一个String对象表示"hello".然后将其存入常量池
        String s2 = "hello";//再次使用"hello"字面量时会重用对象，s2指向s1创建的对象(常量池中的对象)

        System.out.println("s1:"+s1);//s1:hello
        System.out.println("s2:"+s2);//s2:hello

        Class<?> cls = String.class;
        //获取String内部用于保存字符串的char数组value
        Field filed = cls.getDeclaredField("value");
        filed.setAccessible(true);//打开访问权限
        char[] array = (char[])filed.get(s1);//获取s1字符串中value属性的值
        System.out.println(Arrays.toString(array));//[h,e,l,l,o]

        char[] array2 ={'j','a','v','a','!','!'};//"java!!"
        filed.set(s1,array2);

        System.out.println("s1:"+s1);//s1:java!!
        System.out.println("s2:"+s2);//s2:java!!

        String s3 = "hello";
        System.out.println("s3:"+s3);//s3:java!!
        System.out.println("hello");//java!!
        System.out.println("hello"+"world");//预编译属性
        String s = ",";
        System.out.println("hello"+s+"world");

    }
}
